package com.ctsi.ftp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.HashSet;
import java.util.Set;

public class FileTools {
	
	/**
	 * 复制文件
	 * @param srcFile
	 * @param destDir
	 * @param newFileName
	 * @return
	 */
	public static long copyFile(File srcFile, File destDir, String newFileName) {
		long copySizes = 0;
		if (!srcFile.exists()) {
			System.out.println("源文件不存在");
			copySizes = -1;
		} else if (newFileName == null) {
			System.out.println("文件名为null");
			copySizes = -1;
		} else {
			if ((!destDir.exists()) || (!destDir.isDirectory())) {
				System.out.println("目标目录不存在");
				destDir.mkdir();
			}
			FileChannel fcin = null;
			FileChannel fcout = null;
			try {
				fcin = new FileInputStream(srcFile).getChannel();
				fcout = new FileOutputStream(new File(destDir, newFileName))
						.getChannel();
				// ByteBuffer buff = ByteBuffer.allocate(1024);
				int i = 0;
				long t1 = System.currentTimeMillis();

				long size = fcin.size();
				// fcin.transferTo(first,length,fcout);
				fcout.transferFrom(fcin, 0, fcin.size());
				// 一定要分清哪个文件有数据，那个文件没有数据，数据只能从有数据的流向
				// 没有数据的文件
				long t2 = System.currentTimeMillis();
				// fcin.close();
				// fcout.close();
				copySizes = size;
				long t = t2 - t1;
				// System.out.println("复制了" + i + "个字节\n" + "时间" + t);
				// System.out.println("复制了" + size + "个字节\n" + "时间" + t);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (fcin != null) {
					try {
						fcin.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (fcout != null) {
					try {
						fcout.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return copySizes;
	}

	/**
	 * 将用逗号隔开的数字字符串,转化为数字集合
	 * 
	 * @param filter
	 * @return
	 */
	public static Set<Integer> changerFilter(String filter) {
		Set<Integer> result = new HashSet<Integer>();

		try {
			String[] filters = filter.split(",");
			if (filters != null) {
				for (String temp : filters) {
					if (temp == null) {
						continue;
					}
					try {
						result.add(Integer.parseInt(temp));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 判断第一个"."前面的数字，是否在传入的集合中
	 * 
	 * @param fileName
	 * @param filters
	 * @return
	 */
	public static boolean accept(String fileName, Set<Integer> filters) {
		if (filters == null) {
			return false;
		}
		try {
			int subNum = fileName.indexOf(".");
			String temp = fileName.substring(subNum - 1, subNum);
			if (filters.contains(Integer.parseInt(temp))) {
				return true;
			}
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println("===== error ===== fileName "+fileName +" could not filter!");
		}
		return false;
	}

}
